use std::path::PathBuf;
use std::env;
use std::fs;

fn main() {
    // 构建CUDA库
    let dst = cmake::build("cuda");
    
    // 链接CUDA库
    println!("cargo:rustc-link-search=native={}", dst.display());
    println!("cargo:rustc-link-lib=dylib=rust_cuda_kernels");
    
    // 设置运行时链接路径
    if let Ok(cuda_path) = env::var("CUDA_LIBRARY_PATH") {
        println!("cargo:rustc-link-search=native={}", cuda_path);
    }
    println!("cargo:rustc-link-lib=dylib=cudart");
    
    // 复制生成的共享库到target目录
    let out_dir = env::var("OUT_DIR").unwrap();
    let lib_name = if cfg!(target_os = "windows") {
        "rust_cuda_kernels.dll"
    } else if cfg!(target_os = "macos") {
        "librust_cuda_kernels.dylib"
    } else {
        "librust_cuda_kernels.so"
    };
    
    let src = PathBuf::from(dst).join("lib").join(lib_name);
    let dest = PathBuf::from(out_dir).join(lib_name);
    
    fs::copy(&src, &dest).expect("Failed to copy shared library");
    println!("cargo:rustc-env=CUDA_LIB_PATH={}", dest.display());
}